home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / nroff1.zip / NROCMD.C < prev    next >
C/C++ Source or Header  |  1990-12-07  |  17KB  |  654 lines

  1. /*
  2.  *      Command processor for NRO text processor
  3.  *
  4.  *      Stephen L. Browning
  5.  *      5723 North Parker Avenue
  6.  *      Indianapolis, Indiana 46220
  7.  *
  8.  *    Ported to MS C 5.1 
  9.  *      by John Dudeck (jdudeck@polyslo.calpoly.edu) 11/25/90.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include "nro.h"
  14. #include "nroxtrn.h"
  15.  
  16. char *skipbl();
  17. char *skipwd();
  18.  
  19. comand(p)
  20. char *p;
  21. {
  22.         int ct, val;
  23.         int spval;
  24.         int index;
  25.         char argtyp;
  26.         char name[MAXLINE];
  27.         char macexp[MXMLEN];
  28.  
  29.         ct = comtyp(p,macexp);
  30.         if (ct == UNKNOWN) {
  31.                 printf("*** nro: unrecognized command %s\n",p);
  32.                 return;
  33.         }
  34.         expesc(p,name);
  35.         val = getval(p,&argtyp);
  36.         switch (ct) {
  37.         case BO: /* bold face */
  38.                 set(&dc.boval,val,argtyp,1,0,HUGE);
  39.                 dc.cuval = dc.ulval = 0;
  40.                 break;
  41.         case BP: /* begin page */
  42.                 if(pg.lineno > 0) space(HUGE);
  43.                 set(&pg.curpag,val,argtyp,pg.curpag+1,-HUGE,HUGE);
  44.                 pg.newpag = pg.curpag;
  45.                 break;
  46.         case BR: /* break */
  47.                 brk();
  48.                 break;
  49.         case BS: /* backspaces in output */
  50.                 set(&dc.bsflg,val,argtyp,1,0,1);
  51.                 break;
  52.         case CC: /* command character */
  53.                 if (argtyp == '\r' || argtyp == '\n') dc.cmdchr = '.';
  54.                 else dc.cmdchr = argtyp;
  55.                 break;
  56.         case CE: /* center */
  57.                 brk();
  58.                 set(&dc.ceval,val,argtyp,1,0,HUGE);
  59.                 break;
  60.         case CU: /* continuous underline */
  61.                 set(&dc.cuval,val,argtyp,1,0,HUGE);
  62.                 dc.ulval = dc.boval = 0;
  63.                 break;
  64.         case DE: /* define macro */
  65.                 defmac(p,sofile[dc.flevel]);
  66.                 break;
  67.         case EF: /* even footer */
  68.                 gettl(p,pg.efoot,&pg.eflim[0]);
  69.                 break;
  70.         case EH: /* even header */
  71.                 gettl(p,pg.ehead,&pg.ehlim[0]);
  72.                 break;
  73.         case EN: /* end macro definition */
  74.                 puts("***nro: missing .de command\n");
  75.                 break;
  76.         case FI: /* fill */
  77.                 brk();
  78.                 dc.fill = YES;
  79.                 break;
  80.         case FO: /* footer */
  81.                 gettl(p,pg.efoot,&pg.eflim[0]);
  82.                 gettl(p,pg.ofoot,&pg.oflim[0]);
  83.                 break;
  84.         case HE: /* header */
  85.                 gettl(p,pg.ehead,&pg.ehlim[0]);
  86.                 gettl(p,pg.ohead,&pg.ohlim[0]);
  87.                 break;
  88.         case IN: /* indenting */
  89.                 set(&dc.inval,val,argtyp,0,0,dc.rmval-1);
  90.                 dc.tival = dc.inval;
  91.                 break;
  92.         case JU: /* justify */
  93.                 dc.juval = YES;
  94.                 break;
  95.         case LS: /* line spacing */
  96.                 set(&dc.lsval,val,argtyp,1,1,HUGE);
  97.                 break;
  98.         case M1: /* set topmost margin */
  99.                 set(&pg.m1val,val,argtyp,2,0,HUGE);
  100.                 break;
  101.         case M2: /* set second top margin */
  102.                 set(&pg.m2val,val,argtyp,2,0,HUGE);
  103.                 break;
  104.         case M3: /* set first bottom margin */
  105.                 set(&pg.m3val,val,argtyp,2,0,HUGE);
  106.                 pg.bottom = pg.plval - pg.m4val - pg.m3val;
  107.                 break;
  108.         case M4: /* set bottom-most margin */
  109.                 set(&pg.m4val,val,argtyp,2,0,HUGE);
  110.                 pg.bottom = pg.plval - pg.m4val - pg.m3val;
  111.                 break;
  112.         case MACRO: /* macro expansion */
  113.                 maceval(p,macexp);
  114.                 break;
  115.         case NE: /* need n lines */
  116.                 brk();
  117.                 if ((pg.bottom-pg.lineno+1) < (val*dc.lsval)) {
  118.                         space(HUGE);
  119.                 }
  120.                 break;
  121.         case NF: /* no fill */
  122.                 brk();
  123.                 dc.fill = NO;
  124.                 break;
  125.         case NJ: /* no justify */
  126.                 dc.juval = NO;
  127.                 break;
  128.         case NR: /* set number register */
  129.                 p = skipwd(p);
  130.                 p = skipbl(p);
  131.                 if (!isalpha(*p)) {
  132.                         puts("***nro: invalid or missing number register name\n");
  133.                 }
  134.                 else {
  135.                         index = tolower(*p) - 'a';
  136.                         p = skipwd(p);
  137.                         val = getval(p,&argtyp);
  138.                         set(&dc.nr[index],val,argtyp,0,-HUGE,HUGE);
  139.                 }
  140.                 break;
  141.         case OF: /* odd footer */
  142.                 gettl(p,pg.ofoot,&pg.oflim[0]);
  143.                 break;
  144.         case OH: /* odd header */
  145.                 gettl(p,pg.ohead,&pg.ohlim[0]);
  146.                 break;
  147.         case PC: /* page number character */
  148.                 if (argtyp == '\r' || argtyp == '\n') dc.pgchr = EOS;
  149.                 else dc.pgchr = argtyp;
  150.                 break;
  151.         case PL: /* page length */
  152.                 set(&pg.plval,val,argtyp,PAGELEN,
  153.                         pg.m1val+pg.m2val+pg.m3val+pg.m4val+1,HUGE);
  154.                 pg.bottom = pg.plval - pg.m3val - pg.m4val;
  155.                 break;
  156.         case PO: /* page offset */
  157.                 set(&pg.offset,val,argtyp,0,0,HUGE);
  158.                 break;
  159.         case RM: /* right margin */
  160.                 set(&dc.rmval,val,argtyp,PAGEWIDTH,dc.tival+1,HUGE);
  161.                 break;
  162.         case SO: /* source file */
  163.                 p = skipwd(p);
  164.                 p = skipbl(p);
  165.                 if (getwrd(p,name) == 0) break;
  166.                 if (dc.flevel+1 >= NFILES) {
  167.                         puts("***nro: .so commands nested too deeply\n");
  168.                         exit(-1);
  169.                 }
  170.                 if ((sofile[dc.flevel+1] = fopen(name,"r")) == NULL) {
  171.                         printf("***nro: unable to open %s\n",name);
  172.                         exit(-1);
  173.                 }
  174.                 ++dc.flevel;
  175.                 break;
  176.         case SP: /* space */
  177.                 set(&spval,val,argtyp,1,0,HUGE);
  178.                 space(spval);
  179.                 break;
  180.         case TI: /* temporary indent */
  181.                 brk();
  182.                 set(&dc.tival,val,argtyp,0,0,dc.rmval);
  183.                 break;
  184.         case UL: /* underline */
  185.                 set(&dc.ulval,val,argtyp,0,1,HUGE);
  186.                 dc.cuval = dc.boval = 0;
  187.                 break;
  188.         }
  189. }
  190.  
  191.  
  192.  
  193. /*
  194.  *      convert ascii character to decimal.
  195.  */
  196.  
  197. atod(c)
  198. char c;
  199. {
  200.         return(((c < '0') || (c > '9')) ? -1 : c-'0');
  201. }
  202.  
  203.  
  204.  
  205. /*
  206.  *      end current filled line
  207.  */
  208.  
  209. brk()
  210. {
  211.         if(co.outp > 0) {
  212.             /*  co.outbuf[co.outp] = '\r'; 
  213.                 co.outbuf[co.outp + 1] = '\n';
  214.                 co.outbuf[co.outp + 2] = EOS; */
  215.                 
  216.                 co.outbuf[co.outp] = '\n';
  217.                 co.outbuf[co.outp + 1] = EOS;
  218.                 put(co.outbuf);
  219.         }
  220.         co.outp = 0;
  221.         co.outw = 0;
  222.         co.outwds = 0;
  223. }
  224.  
  225.  
  226. /*
  227.  *      Collect macro definition from input stream
  228.  */
  229.  
  230. colmac(p,d,i)
  231. char *p, d[];
  232. int i;
  233. {
  234.         while (*p != EOS) {
  235.                 if (i >= MXMLEN-1) {
  236.                         d[i-1] = EOS;
  237.                         return(ERR);
  238.                 }
  239.                 d[i++] = *p++;
  240.         }
  241.         d[i] = EOS;
  242.         return(i);
  243. }
  244.  
  245.  
  246.  
  247.  
  248. /*
  249.  *      decodes nro command and returns its associated
  250.  *      value.
  251.  */
  252.  
  253. comtyp(p,m)
  254. char *p;
  255. char *m;
  256. {
  257.         char c1, c2;
  258.         char macnam[MNLEN];
  259.         char *s;
  260.  
  261.         p++;
  262.         /*
  263.         *       First check to see if the command is a macro.
  264.         *       If it is, truncate to two characters and return
  265.         *       expansion in m.  Note that upper and lower case
  266.         *       characters are handled differently for macro names,
  267.         *       but not for normal command names.
  268.         */
  269.         getwrd(p,macnam);
  270.         macnam[2] = EOS;
  271.         if ((s = getmac(macnam)) != NULL) {
  272.